package cn.wizzer.app.web.modules.controllers.platform.user.chat.group;

import cn.wizzer.app.user.modules.models.GroupMember;
import cn.wizzer.app.user.modules.models.enums.MsgTypeEnum;
import cn.wizzer.app.user.modules.models.enums.OprTypeEnum;
import cn.wizzer.app.user.modules.services.GroupLogService;
import cn.wizzer.app.user.modules.services.GroupMemberService;
import cn.wizzer.app.user.modules.services.UserService;
import cn.wizzer.framework.base.Result;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

@IocBean
@At("/platform/user/group/member")
public class GroupMemberController {
    private static final Log log = Logs.get();

    @Inject
    @Reference
    private GroupMemberService groupMemberService;
    @Inject
    @Reference
    private GroupLogService groupLogService;
    @Inject
    @Reference
    private UserService userService;

    @At("/")
    @Ok("beetl:/platform/user/chat/group/member.html")
    @RequiresPermissions("user.group.member")
    public void index() {
    }

    /**
     * 查询（分页）
     *
     * @return Object
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("user.group.member")
    public Object data(@Param("groupName") String groupName,
                       @Param("userKeyword") String userKeyword,
                       @Param("searchDate") String[] searchDate,
                       @Param("pageNumber") int pageNumber,
                       @Param("pageSize") int pageSize) {
        return Result.success().addData(groupMemberService.listPage(pageNumber, pageSize, groupName, userKeyword, searchDate));
    }

    /**
     * 伪删除
     *
     * @return Object
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("user.group.member.edit")
    public Object del(String id) {
        GroupMember groupMember = groupMemberService.fetch(id);
        //添加消息记录
        String content = "管理员移除了 " + userService.fetch(groupMember.getUserId()).getNickName();
        groupLogService.groupSend(groupMember.getUserId(), groupMember.getGroupId(), content, MsgTypeEnum.TEXT, OprTypeEnum.REMOVED, true);
        groupMemberService.vDelete(id);
        return Result.success();
    }

}
